package com.service;

import static com.def.GenericDef.E_XPBISCUS_ALREADY_EXIST;
import static com.def.GenericDef.E_XPBISCUS_INVALID_INPUT;
import static com.def.GenericDef.E_XPBISCUS_IS_NOT_FOUND;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPBISCUS;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisCusDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_cus_loc;
import com.model.Bis_cus_locId;
import com.tx.Xpbiscus.XpbiscusI;
import com.tx.Xpbiscus.XpbiscusIA;
import com.tx.Xpbiscus.XpbiscusO;
import com.tx.Xpbiscus.XpbiscusOA;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpbiscusService")
public class XpbiscusService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpbiscusService.class);
	private static String TX_NAME = T_XPBISCUS;

	private XpbiscusI inTrx;
	private XpbiscusO outTrx;
	private MqOpe mqOpe;

	@Autowired
	private IBisCusDao bisCusDao;
	
	private String gszMsgBuf = "";

	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;

	public String subMainProc(String strInMsg) {
		Session session = bisCusDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = null;

		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		inTrx = new XpbiscusI();
		outTrx = new XpbiscusO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);

		mqOpe = new MqOpe();

		try {
			inTrx = (XpbiscusI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}

	/**
	 * Real action type
	 * 
	 * @param actionType
	 * @return
	 */
	private long subMainProc2(char actionType) {
		long lRc = _NORMAL;
		switch (actionType) {
		case 'Q': {
			lRc = getCusInfo();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'U': {
			lRc = updateCusLocInfo();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'D': {
			lRc = deleteCusLocInfo();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'A': {
			lRc = addCusLocInfo();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		default: {
			gszMsgBuf = "Invalide type input: [" + actionType + "]";
			return E_XPBISCUS_INVALID_INPUT;
		}
		}

		return lRc;
	}

	private long getCusInfo() {
		long lRc = _NORMAL;
		List<XpbiscusOA> xpbiscusOAList = new ArrayList <XpbiscusOA>();
		String sql = " from Bis_cus_loc WHERE cus_id_fk='"+ inTrx.getIary().get(0).getCus_id_fk()+"'";
		List<Bis_cus_loc> bis_cus_loc_list = bisCusDao.find(sql);
		if( bis_cus_loc_list.size() > 0){
			for( int i =0;i<bis_cus_loc_list.size();i++){
				Bis_cus_loc bis_cus_loc =  new Bis_cus_loc();
				bis_cus_loc = bis_cus_loc_list.get(i);
				xpbiscusOAList.add(subSetOutTrx( bis_cus_loc ));
			}
			outTrx.setOary(xpbiscusOAList);
			outTrx.setTbl_cnt(bis_cus_loc_list.size());
			
		}
		return _NORMAL;
	}
	private long updateCusLocInfo() {
		long lRc = _NORMAL;
		if( inTrx.getTbl_cnt()>0 ){
			for( int i=0;i<inTrx.getTbl_cnt();i++){
				Bis_cus_loc bis_cus_loc = new Bis_cus_loc();
				Bis_cus_locId id = new Bis_cus_locId();
				XpbiscusIA xpbiscusIA = new XpbiscusIA();
				xpbiscusIA = inTrx.getIary().get(i);
				id.setCus_id_fk(xpbiscusIA.getCus_id_fk());
				id.setCus_seq_id(xpbiscusIA.getCus_seq_id());
				bis_cus_loc = bisCusDao.get(id);
				if( bis_cus_loc == null){
					gszMsgBuf = " customer location  [" + xpbiscusIA.getCus_id_fk()
					+ "]is not fund!";
					return E_XPBISCUS_IS_NOT_FOUND;
				}
				bis_cus_loc.setEvt_usr(inTrx.getEvt_user());
				bis_cus_loc.setEvt_timestamp(gEvtTimestamp);
				bis_cus_loc.setLoc_info(xpbiscusIA.getLoc_info());
				bisCusDao.update(bis_cus_loc);
			}
		}
		return lRc;
	}
	private long deleteCusLocInfo() {
		
		long lRc = _NORMAL;
		if( inTrx.getTbl_cnt()>0 ){
			for( int i=0;i<inTrx.getTbl_cnt();i++){
				Bis_cus_locId id = new Bis_cus_locId();
				id.setCus_id_fk(inTrx.getIary().get(i).getCus_id_fk());
				id.setCus_seq_id(inTrx.getIary().get(i).getCus_seq_id());
				bisCusDao.deleteByKey(id);
			}
		}
		
		return lRc;
	}
	private long addCusLocInfo() {
		long lRc = _NORMAL;
		if( inTrx.getTbl_cnt()>0 ){
			for( int i=0;i<inTrx.getTbl_cnt();i++){
				Bis_cus_loc bis_cus_loc = new Bis_cus_loc();
				Bis_cus_locId id = new Bis_cus_locId();
				XpbiscusIA xpbiscusIA = new XpbiscusIA();
				xpbiscusIA = inTrx.getIary().get(i);
				id.setCus_id_fk(xpbiscusIA.getCus_id_fk());
				id.setCus_seq_id(xpbiscusIA.getCus_seq_id());
				bis_cus_loc = bisCusDao.get(id);
				if( bis_cus_loc !=null){
					gszMsgBuf = " customer location  [" + xpbiscusIA.getCus_id_fk()
					+ "]is already exist!";
					return E_XPBISCUS_ALREADY_EXIST;
				}
				bis_cus_loc = new Bis_cus_loc();
				bis_cus_loc.setId(id);
				bis_cus_loc.setEvt_usr(inTrx.getEvt_user());
				bis_cus_loc.setEvt_timestamp( gEvtTimestamp );
				bis_cus_loc.setLoc_typ(xpbiscusIA.getLoc_type());
				bis_cus_loc.setLoc_info(xpbiscusIA.getLoc_info());
				bisCusDao.save(bis_cus_loc);
			}
		}
		return lRc;
	}
	public XpbiscusOA subSetOutTrx( Bis_cus_loc  bis_cus_loc ){
		XpbiscusOA  xpbiscusOA = new XpbiscusOA();
		xpbiscusOA.setCus_id_fk(bis_cus_loc.getId().getCus_id_fk());
		xpbiscusOA.setCus_seq_id(bis_cus_loc.getId().getCus_seq_id());
		xpbiscusOA.setLoc_type(bis_cus_loc.getLoc_typ());
		xpbiscusOA.setLoc_info(bis_cus_loc.getLoc_info());
		return xpbiscusOA;
	}
}
